package com.baidu.user.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baidu.user.entity.Class;
import com.baidu.user.entity.Item;
import com.baidu.user.entity.Through;
import com.baidu.user.entity.User;
import com.baidu.user.entity.UserBasicInfo;
import com.baidu.user.entity.UserCustom;
import com.baidu.user.entity.UserType;
import com.baidu.user.service.ClassService;
import com.baidu.user.service.ItemService;
import com.baidu.user.service.ThroughsService;
import com.baidu.user.service.UserService;
import com.baidu.user.service.UserTypeService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 
	 * @param loginname
	 * @param password
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws ServletException
	 *             登陆
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public void Login(String loginname, String password,
			HttpServletResponse response, HttpServletRequest request)
			throws IOException, ServletException {
		User user = userService.login(loginname, password);
		if (user == null) {
			response.getWriter().print(0);
		} else {
			request.getSession().setAttribute("user", user);
			response.getWriter().print(1);
		}
	}

	/**
	 * 
	 * @return 登陆成功
	 */
	@RequestMapping(value = "loginGo.do", method = RequestMethod.POST)
	public String loginGo() {
		return "main";
	}

	/**
	 * 
	 * 注册用户
	 */
	@RequestMapping("register.do")
	@ResponseBody
	public void register(User model) {
		userService.register(model);
	}

	/**
	 * 
	 * 跳转至个人信息界面
	 */
	@RequestMapping(value = "upPersonInfo.do", method = RequestMethod.GET)
	public String upPersonInfo() {
		return "/user/personInfo";
	}

	/**
	 * 
	 * 展示个人信息
	 */
	@RequestMapping("loadPersonInfo.do")
	@ResponseBody
	public UserCustom loadPersonInfo(HttpServletRequest request) {
		User sessionUser = (User) request.getSession().getAttribute("user");
		BigDecimal id = sessionUser.getId();
		System.out.println(id);
		UserCustom user = userService.findById(id);
		return user;
	}

	/**
	 * 修改个人信息
	 */
	@RequestMapping(value = "upPersonInfo.do", method = RequestMethod.POST)
	@ResponseBody
	public void upPersonInfo2(User model) {
		userService.update(model);
	}

	@Autowired
	private ClassService classService;

	/**
	 * 获取全部班级
	 */
	@RequestMapping("getClasses.do")
	@ResponseBody
	public List<Class> getClasses() {
		List<Class> classAll = classService.getClassAll();
		return classAll;
	}

	@Autowired
	private UserTypeService userTypeService;

	/**
	 * 获取全部用户类型
	 */
	@RequestMapping("getTypes.do")
	@ResponseBody
	public List<UserType> getTypes() {
		List<UserType> userTypeAll = userTypeService.getUserTypeAll();
		return userTypeAll;
	}

	/**
	 * 
	 * 注销
	 */
	@RequestMapping("outLogin.do")
	public String outLogin(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/login.jsp";
	}

	@Autowired
	private ThroughsService throughService;

	/**
	 * look through 查看工作经历
	 * 
	 * @throws IOException
	 */
	@RequestMapping("lookThroughs.do")
	@ResponseBody
	public Map<String, Object> lookThroughs(HttpSession session, Integer page,
			Integer rows, String sort, String order) {
		User user = (User) session.getAttribute("user");
		if (user != null && user.getId() != null) {
			BigDecimal id = user.getId();
			Map<String, Object> data = throughService.findThroughsByPage(rows,
					page, id, sort, order);
			return data;
		}
		return null;
	}

	/**
	 * look through 查看工作经历
	 * 
	 * @throws IOException
	 */
	@RequestMapping("toLookThroughs.do")
	public String toLookThroughs() {
		return "/user/throughsList";
	}

	@RequestMapping("/saveThroughs.do")
	@ResponseBody
	public void saveThroughs(Through through, HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user != null & user.getId() != null) {
			BigDecimal id = user.getId();
			through.setUserid(id);
			throughService.save(through);
		}
	}

	@Autowired
	private ItemService itemService;

	/**
	 * 
	 * 到个人项目列表展示页面
	 */
	@RequestMapping("toItems.do")
	public String toItems(){
		return "/user/items";
	}
	/**
	 * 获取个人项目的json数据
	 * @param session
	 * @return
	 */
	@RequestMapping("getItems.do")
	@ResponseBody
	public List<Item> getItems(HttpSession session) {
		User user = (User) session.getAttribute("user");
		if (user != null && user.getId() != null) {
			return itemService.getItems(user.getId());
		} else {
			return null;
		}
	}
	/**
	 * 
	 * 到公开项目列表页面
	 */
	@RequestMapping("toItemsPublic.do")
	public String toItemsPulic(){
		return "/user/itemsPublic";
	}
	/**
	 * 获取公开项目的json数据
	 * @return
	 */
	@RequestMapping("getItemsPublic.do")
	@ResponseBody
	public List<Item> getItemsPublic() {
		return itemService.getItemsPublic();
	}
	/**
	 * 修改项目
	 * @param item
	 */
	@RequestMapping("updateItem.do")
	@ResponseBody
	public void updateItem(Item item){
		itemService.updateItem(item);
	}
	/**
	 * 添加项目
	 * @param item
	 * @param session
	 */
	@RequestMapping("addItem.do")
	@ResponseBody
	public void addItem(Item item,HttpSession session){
		User user = (User) session.getAttribute("user");
		if(user!=null){
			BigDecimal userid = user.getId();
			item.setUserid(userid);
			if(item.getIspublic()==null){
				item.setIspublic(new BigDecimal(0));
			}
			itemService.saveItem(item);
			
		}
	}
	@RequestMapping("toResume.do")
	public ModelAndView toResume(HttpSession session){
		ModelAndView mav = new ModelAndView();
		User user = (User) session.getAttribute("user");
		if(user!=null){
			UserBasicInfo basicinfo = userService.findBasicUserById(user.getId()); 
			List<Through> throughs = throughService.findPersonageThrough(user.getId());
			List<Item> items = itemService.findPersonageItems(user.getId());
			mav.addObject("basicinfo", basicinfo);    
			mav.addObject("throughs", throughs);    
			mav.addObject("items", items);    
		}
		mav.setViewName("/user/resume");
		return mav;
	}
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {

		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				new SimpleDateFormat("yyyy-MM-dd"), true));
	}

}
